<!-- 新增/修改弹框 -->
<template>
  <!-- 新增/修改弹窗 -->
  <el-dialog
    v-model="dialogAddOrEdit.visible"
    :width="900"
    :title="dialogAddOrEdit.title"
    draggable
    close-icon="CloseBold"
    @close="closeDialog(false)"
  >
    <el-form ref="addOrEditFormRef" v-loading="loading" :model="formData" :rules="rules" label-width="100" size="">
      <el-row justify="start" class="d_left">
        <el-col :span="12" class="">
          <el-form-item label="项目名称" prop="projectId">
            <el-input v-model="projectInfo.name" disabled placeholder="自动关联" />
            <el-button
              v-show="dialogAddOrEdit.id == 0 || dialogAddOrEdit.id == undefined"
              type="primary"
              plain
              style="position: absolute; right: 0px"
              @click="handleSelectProject"
              >选择</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="项目编号">
            <el-input v-model="projectInfo.prjNum" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="银行账号">
            <el-input v-model="formData.bankNo" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户名称" prop="accountId">
            <el-input v-model="formData.accountName" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="账户余额">
            <el-input v-model="formData.balance" disabled placeholder="自动关联" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="银行流水号" prop="bankSerialNumber">
            <el-input v-model="formData.bankSerialNumber" placeholder="请输入银行流水号" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="收支信息" prop="incomeType">
            <el-select v-model="formData.incomeType">
              <el-option v-for="item in incomeType" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="">
          <el-form-item label="交易类型" prop="tradeType">
            <el-select v-model="formData.tradeType">
              <el-option v-for="item in tradingType" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="对方户名" prop="oppositeName">
            <el-input v-model="formData.oppositeName" placeholder="请输入对方户名" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="对方账号" prop="oppositeAccount">
            <el-input v-model="formData.oppositeAccount" placeholder="请输入对方账户" />
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="收支金额" prop="amount">
            <money-input v-model="formData.amount" unit="万元" class="input300"></money-input>
            <!-- <el-input v-model="formData.amount" placeholder="请输入收支金额" type="number" /> -->
          </el-form-item>
        </el-col>
        <el-col :span="12" class="">
          <el-form-item label="收支日期" prop="incomeDate">
            <el-date-picker
              v-model="formData.incomeDate"
              type="date"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
              placeholder="选择日期"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <select-project :dialog-select="dialogSelectProject" @select-send-close="projectSendClose"></select-project>
    <template #footer>
      <div class="dialog_footer">
        <el-button type="primary" :loading="loading" @click="handleSubmit">{{ dialogAddOrEdit.footBtn }}</el-button>
        <el-button @click="closeDialog(false)">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import MoneyInput from "@/components/MoneyInput/index.vue";
import { Except, Ride } from "@/utils/decimal";
import { ProjectSpecialAccountPaymentEditForm } from "@/api/specialaccount/types";
import {
  GetProjectSpecialAccountPayment,
  CreateProjectSpecialAccountPayment,
  UpdateProjectSpecialAccountPayment,
  GetProjectSpecialAccountByProjectId,
} from "@/api/specialaccount/index";
import { getDicts } from "@/store/modules/dict";
import { FormRules } from "element-plus";
import SelectProject from "@/views/common/SelectProject/index.vue";

//声明下拉数据
const tradingType = getDicts("tradingType");
const incomeType = getDicts("incomeType");

const dialogSelectProject = reactive({
  visible: false,
  title: "选择项目",
  footBtn: "确认",
  id: 0,
  type: "allproject",
});
const handleSelectProject = function () {
  dialogSelectProject.visible = true;
};
const projectSendClose = function (info: any) {
  dialogSelectProject.visible = false;
  if (info) {
    projectInfo.value = info;
    formData.projectId = info.projectId;
    if (info.projectId) {
      GetProjectSpecialAccountByProjectId(info.projectId).then((res: any) => {
        if (res.success) {
          let data = res.data;
          formData.projectId = data.projectId;
          formData.accountId = data.id;
          formData.bankNo = data.bankNo;
          formData.balance = data.balance;
          formData.accountName = data.username;
        } else {
          formData.projectId = undefined;
          formData.accountId = undefined;
          formData.bankNo = undefined;
          formData.balance = undefined;
          formData.accountName = undefined;
        }
      });
    }
  }
};

const props = defineProps({
  dialogAddOrEdit: {
    type: Object,
    default: () => ({
      visible: false,
      title: "创建收支信息",
      footBtn: "保存",
      id: 0,
    }),
  },
});

const { dialogAddOrEdit } = toRefs(props);

const projectInfo = ref<any>({});

const emit = defineEmits(["sendClose"]);

const loading = ref(false);
const formData = reactive<ProjectSpecialAccountPaymentEditForm>({});
const addOrEditFormRef = ref(ElForm);

const rules = reactive<FormRules<ProjectSpecialAccountPaymentEditForm>>({
  projectId: [{ required: true, message: "请选择存在专户的项目", trigger: "blur" }],
  bankSerialNumber: [{ required: true, message: "请输入流水号", trigger: "blur" }],
  incomeType: [{ required: true, message: "请选择收支类型", trigger: "blur" }],
  tradeType: [{ required: true, message: "请选择交易类型", trigger: "blur" }],
  oppositeName: [{ required: true, message: "请输入对方账户", trigger: "blur" }],
  oppositeAccount: [{ required: true, message: "请输入对方账户", trigger: "blur" }],
  amount: [{ required: true, message: "请输入收支金额", trigger: "blur" }],
});

onMounted(() => {
  initForm();
});

const initForm = () => {
  if (dialogAddOrEdit.value.id) {
    loading.value = true;
    GetProjectSpecialAccountPayment(dialogAddOrEdit.value.id)
      .then((res) => {
        if (res.type == 0) {
          if (res.success) {
            res.data.amount = Except(res.data.amount, 10000);
            Object.assign(formData, res.data);
            projectInfo.value.name = res.data.projectName;
            projectInfo.value.prjNum = res.data.projectNum;
          } else {
          }
        } else {
        }
      })
      .finally(() => {
        loading.value = false;
      });
  }
};

/**
 * 关闭弹窗
 */
function closeDialog(query: boolean, data?: any) {
  emit("sendClose", query, data);
  resetForm();
}

/**
 * 重置表单
 */
function resetForm() {
  addOrEditFormRef.value.resetFields();
  addOrEditFormRef.value.clearValidate();
  formData.id = undefined;
}

/**
 * 项目表单提交
 */
function handleSubmit() {
  addOrEditFormRef.value.validate((valid: any) => {
    if (valid) {
      let submitForm = Object.assign({}, formData);
      submitForm.amount = Ride(formData?.amount ?? 0, 10000);
      if (dialogAddOrEdit.value.id && dialogAddOrEdit.value.id > 0) {
        loading.value = true;
        UpdateProjectSpecialAccountPayment(submitForm)
          .then((res) => {
            if (res.type == 0) {
              if (res.success) {
                closeDialog(true);
              } else {
              }
            } else {
            }
          })
          .finally(() => (loading.value = false));
        return;
      }
      loading.value = true;
      CreateProjectSpecialAccountPayment(submitForm)
        .then((res) => {
          if (res.type == 0) {
            if (res.success) {
              closeDialog(true);
            } else {
            }
          } else {
          }
        })
        .finally(() => (loading.value = false));
    }
  });
}
</script>

<style lang="scss" scoped>
.d_left {
  margin-left: 10px;
}
:deep(.el-form-item) {
  flex-wrap: wrap;
}
.dialog_footer {
  // display: flex;
  // justify-content: center;
}

:deep(.el-input) {
  width: 80%;
}

:deep(.el-select) {
  width: 100%;
}

:deep(.el-input__wrapper) {
  width: 100%;
}

:deep(.el-form-item__label) {
  color: #696868;
  font-size: 13px;
}
</style>
